<?php

/**
 * Framework_Module_User
 *
 * @author      Ian Szewczyk <roamzero@gmail.com>
 * @copyright   (c) 2007 Ian Szewczyk. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package Module
 * @filesource
 */

/**
 * Framework_Module_User
 *
 * This is the base class for user related functions
 *
 * @author Ian Szewczyk <roamzero@gmail.com>
 * @package Module
 */
class Framework_Module_User extends Framework_Module
{

	/**
     * __construct
     *
     * Initializes the module: sets the default presenter, the allowed
     * conrollers, and the allowed events
     *
     * @access 		public
     * @return 		void
     */
	public function __construct()
	{
		$this->controllers[] = 'Peopletab';
		parent::__construct();
		$this->presenter = 'None';
	}

	/**
     * __default
     * 
     *
     * @access 		public
     * @return 		mixed 		boolean true on success, PEAR_Error on failure
     */
	public function __default()
	{
			// Does nothing!

	}

	/**
     * sessionLogin
     * 
     *
     * @access 		public
     * @param		integer		$idSiteUser		ID from SiteUser table to bind to session
     * @return 		void
     */
	public function sessionLogin($idSiteUser)
	{
		$this->session->idSiteUser = $idSiteUser;
		// Set dateLastLogin
		$siteUserTable = Framework_DB::tableFactory('SiteUser');
		$date = time();
		$siteUserTable->update(array('dateLastLogin' => $date), 'idSiteUser='.$idSiteUser);
		//$sql = 'UPDATE siteuser SET dateLastLogin ='.$date.' WHERE idSiteUser='.$idSiteUser;

	}

	


	/**
     * verifyToken
     * 
     *
     * @access 		protected
     * @param		string		$pword			Owner's password
     * @param		string		$ip				IP address
     * @param		string		$private_key	Owner's private key
     * @param		string		$token			Token to verify
     * @return 		boolean
     */
	protected function verifyToken($pword, $ip, $private_key, $token)
	{
		require_once 'Crypt/RSA.php';
		require_once 'Crypt/RSA/Key.php';

		if (!strlen($private_key))
			return false;

		$key = Crypt_RSA_Key::fromString($private_key);
		$rsa_obj = new Crypt_RSA;
		$rsa_obj->setParams(array('dec_key' => $key));
		$value = $rsa_obj->decrypt($token);
			
		if (!$rsa_obj->isError() && $value == md5($pword.$ip))
			return true;
		else {
			return false;
		}

	}

	/**
     * __destruct
     * 
     *
     * @access 		public
     * @return 		void
     */
	public function __destruct()
	{
		parent::__destruct();
	}
}

?>